<?php
namespace app\api\controller;

use app\common\controller\Backend;
use app\common\library\Utils;
use app\common\model\Staff;
use think\db\exception\DbException;

/**
 * 车辆管理
 */
class Car extends Backend
{
    /**
     * Car模型对象
     * @var object
     * @phpstan-var \app\common\model\Car
     */
    protected object $model;

    protected array|string $preExcludeFields = ['id', 'create_time', 'update_time'];

    protected string|array $quickSearchField = ['id'];

    protected array $noNeedPermission = ['lists', 'save', 'add', 'edit', 'del', 'index', 'orders', 'orderDetail', 'orderDone', 'subOrder'];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new \app\common\model\Car;
    }


    /**
     * 若需重写查看、编辑、删除等方法，请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
     */
    public function lists(){
        $data = $this->request->post();
        $limit = $this->request->post('limit');
        $where = [];

        $res = \app\common\model\CarView::withSearch(['brand','name','car_no','status', 'keywords'], $data)
            ->where($where)
            ->order('id desc')
            ->paginate($limit);
        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }

    public function orders(){
        $data = $this->request->post();
        $limit = $this->request->post('limit');
        $where = [];
        $res = \app\common\model\Order::withSearch(['car_id'], $data)
            ->where($where)
            ->order('id desc')
            ->paginate($limit);
        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }

    public function orderDetail(){
        $no = $this->request->post('no');
        $res = \app\common\model\Order::where('no', $no)->with(['car', 'startStore', 'startStaff', 'completeStaff'])->find();
        $this->success('', $res);
    }

    public function subOrder(){
        $data = $this->request->post();
        $data['no'] = Utils::createNo();
        $data['create_time'] = time();
        $data['update_time'] = time();
        $staff = Staff::where('admin_id', $this->auth->id)->find();
        $data['start_staff_id'] = $staff->id;
        $data['order_status'] = 1;
        try {
            $order = \app\common\model\Order::insert($data);

            \app\common\model\Car::update([
                'status' => 2,
                'id' => $data['car_id']
            ]);
            $this->success('操作成功');
        } catch (DbException $e) {
            $this->error('操作失败', [$e->getMessage()]);
        }
    }

    public function orderDone() {
        $no = $this->request->post('no');
        $order = \app\common\model\Order::where('no', $no)->findOrEmpty();
        if($order->isEmpty()){
            $this->error('订单无效');
        }

        if($order->order_status >= 2){
            $this->error('订单已完成了');
        }
        $data = $this->request->post();
        $data['complete_date'] = date('y-m-d H:i:s');
        $staff = Staff::where('admin_id', $this->auth->id)->find();
        $data['complete_staff_id'] = $staff->id;
        $data['id'] = $order->id;
        $data['order_status'] = 2;
        try {
            \app\common\model\Order::update($data);
            \app\common\model\Car::update([
                'status' => 1,
                'id' => $order->car_id
            ]);
            $this->success('操作成功');
        } catch (DbException $e) {
            $this->error('操作失败', [$e->getMessage()]);
        }
    }
}